package com.xhdx.ati.adau.controller.demo;

import java.util.List;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xhdx.ati.adau.controller.service.flow.ActiFlowService;
import com.xhdx.ati.adau.entity.FlowEntity;
import com.xhdx.ati.adau.entity.TaskEntity;
import com.xhdx.ati.adau.util.BaseRespone;
import com.xhdx.ati.adau.util.StringUtils;

@RestController
@RequestMapping("/myactivi/first")
public class MyfirstController {
	@Autowired
	private ActiFlowService actiFlowService;

	@RequestMapping("/str")
	public String list() {

		return "成功";
	}

	/**
	 * 部署流程资源。step1,执行一次就性，更新到数据库中。如果流程图有更新可以再次部署
	 * 部署流程资源有很多种方法，包括classpath、InputStream、字符串、zip格式压缩包，下面举例一种
	 * @param flowEntity
	 * @return
	 */
	@RequestMapping(value = "/deploy")
    public BaseRespone<?> deploy(@RequestBody FlowEntity flowEntity) {
		if(flowEntity == null
    			|| StringUtils.isEmpty(flowEntity.getBpmn())
    			|| StringUtils.isEmpty(flowEntity.getPng())
    			|| StringUtils.isEmpty(flowEntity.getName())){
    		throw new NullPointerException("{bpmn}{png}{name}参数都不能为空");
    	}
		
		Deployment deployment = actiFlowService.deploy( flowEntity);
    	
        return BaseRespone.SUCCESS.msg(flowEntity.getName()+
        		"部署成功:部署ID,"+deployment.getId()+
        		";部署时间,"+deployment.getDeploymentTime());    
    }
	/**
	 * 删除流程定义记录
	 * @param deploymentId
	 * @return
	 */
	@RequestMapping(value = "/deleteProcessDefinition")
    public BaseRespone<?> deleteProcessDefinition(@RequestParam(required = true) String deploymentId) {		
		actiFlowService.deleteProcessDefinition( deploymentId);
    	
        return BaseRespone.SUCCESS;    
    }
	/**
	 * 启动一次实例，step2
	 * @param processDefinitionKey
	 * @return
	 */
	@RequestMapping(value = "/startProcess")
    public BaseRespone<?> startProcess(@RequestParam(required = true) String processDefinitionKey) {		
		String a = actiFlowService.startProcess(processDefinitionKey);
    	
        return BaseRespone.SUCCESS.msg(a);    
    }
	/**
	 * 办理任务，step3，可循环办理任务
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/handleTask")
    public BaseRespone<?> handleTask(@RequestParam(required = true) String taskId) {		
		actiFlowService.handleTask(taskId);
    	
        return BaseRespone.SUCCESS;    
    }
	/**
	 * 查询流程定义，step4
	 * @param processDefinitionKey
	 * @return
	 */
	@RequestMapping(value = "/query")
    public BaseRespone<?> query(@RequestParam(required = true) String processDefinitionKey) {		
		List<ProcessDefinition> a = actiFlowService.query(processDefinitionKey);
    	
        return BaseRespone.SUCCESS.content(a);    
    }
	/**
	 * 组查询自己的任务
	 * @param group
	 * @return
	 */
	@RequestMapping(value = "/groupQueryTasks")
    public BaseRespone<List<TaskEntity>> groupQueryTasks(@RequestParam(required = true) String group) {		
		List<TaskEntity> a = actiFlowService.groupQueryTasks(group);
    	
        return BaseRespone.SUCCESS.content(a);    
    }
	/**
	 * 查询自己的任务
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/userQueryTasks")
    public BaseRespone<List<TaskEntity>> userQueryTasks(@RequestParam(required = true) String user) {		
		List<TaskEntity> a = actiFlowService.userQueryTasks(user);
    	
        return BaseRespone.SUCCESS.content(a);    
    }
	/**
	 * 查询所有的任务
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/queryAllTasks")
    public BaseRespone<List<TaskEntity>> queryAllTasks() {		
		List<TaskEntity> a = actiFlowService.queryAllTasks();
    	
        return BaseRespone.SUCCESS.content(a);    
    }
	/**
     * 拾取任务 组任务分配给个人任务 指定任务的办理人字段，相当于领取任务
     */
	@RequestMapping(value = "/claim")
    public BaseRespone<?> claim(@RequestParam(required = true) String userId, 
    		@RequestParam(required = true) String taskId) {
		actiFlowService.claim(taskId, userId);
        return BaseRespone.SUCCESS.msg("拾取任务成功");
    }
    /**
     * 查询正在执行的某个任务办理人表
     */
	@RequestMapping(value = "/findRunPersonTask")
    public BaseRespone<?> findRunPersonTask(@RequestParam(required = true) String taskId) {
    	List<String> listSinfo = actiFlowService.findRunPersonTask(taskId);

    	return BaseRespone.SUCCESS.content(listSinfo);
    }

    /**
     * 查询历史任务的办理人
     */
	@RequestMapping(value = "/findHistoryPersonTask")
    public BaseRespone<?> findHistoryPersonTask(@RequestParam(required = true) String processInstanceId) {
    	List<String> listSinfo = actiFlowService.findHistoryPersonTask(processInstanceId);

    	return BaseRespone.SUCCESS.content(listSinfo);
    }
}
